home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / benchmarks / hash / hash.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-31  |  2.1 KB  |  83 lines

  1. /* 
  2.  * hash.c --
  3.  *
  4.  *    This file is a benchmark program to measure the cost of a
  5.  *    hash table probe.  It should be invoked as follows:
  6.  *    
  7.  *    hash count [tableSize]
  8.  *
  9.  *    Where count is the number of probes to make and tableSize is
  10.  *    the initial hash table size.  It puts 100 randome entries into
  11.  *    the hash table, makes "count" probes, then prints out the
  12.  *    average time per probe.
  13.  *
  14.  * Copyright 1989 Regents of the University of California
  15.  * Permission to use, copy, modify, and distribute this
  16.  * software and its documentation for any purpose and without
  17.  * fee is hereby granted, provided that the above copyright
  18.  * notice appear in all copies.  The University of California
  19.  * makes no representations about the suitability of this
  20.  * software for any purpose.  It is provided "as is" without
  21.  * express or implied warranty.
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <sys/time.h>
  26. #include <sys/resource.h>
  27. #include <hash.h>
  28. #include <stdlib.h>
  29.  
  30. Hash_Table table;
  31.  
  32. #define STRING_LENGTH 10
  33.  
  34. main(argc, argv)
  35.     int argc;
  36.     char **argv;
  37. {
  38.     Hash_Entry *hPtr;
  39.     int count, i, j;
  40.     char names[100][STRING_LENGTH+1];
  41.     struct timeval start, stop;
  42.     struct timezone tz;
  43.     int micros, tableSize;
  44.     double timePer;
  45.  
  46.     if ((argc != 2) && (argc != 3)){
  47.     fprintf(stderr, "Usage: getpid count [tableSize]\n");
  48.     exit(1);
  49.     }
  50.     count = atoi(argv[1]);
  51.     if (argc == 3) {
  52.     tableSize = atoi(argv[2]);
  53.     } else {
  54.     tableSize = 0;
  55.     }
  56.  
  57.     Hash_InitTable(&table, tableSize, HASH_STRING_KEYS);
  58.     for (i = 0; i < 100; i++) {
  59.     for (j = 0; j < STRING_LENGTH; j++) {
  60.         names[i][j] = 'A' + ((rand() >> 10) & 077);
  61.     }
  62.     names[i][STRING_LENGTH] = 0;
  63.     Hash_CreateEntry(&table, names[i], (Boolean *) NULL);
  64.     }
  65.  
  66.     gettimeofday(&start, (struct timezone *) NULL);
  67.  
  68.     j = 0;
  69.     for (i = 0; i < count; i++) {
  70.     hPtr = Hash_FindEntry(&table, names[j]);
  71.     j++;
  72.     if (j >= 100) {
  73.         j = 0;
  74.     }
  75.     }
  76.  
  77.     gettimeofday(&stop, (struct timezone *) NULL);
  78.     micros = 1000000*(stop.tv_sec - start.tv_sec)
  79.         + stop.tv_usec - start.tv_usec;
  80.     timePer = micros;
  81.     printf("Time per probe: %.2f microseconds\n", timePer/count);
  82. }
  83.